import api from '../../../api';
import { message } from 'antd';
export default {
  namespace: 'yieldPerformanceInProM',

  state: {
    yieldOrgLevelData: [],
    yieldOrgLevelData2: [],
    columns: [
      {
        title: '近一月收益率',
        dataIndex: 'oneMonth',
        key: 'oneMonth',
        align: 'right',
      },
      {
        title: '近三月收益率',
        dataIndex: 'threeMonth',
        key: 'threeMonth',
        align: 'right',
      },
      {
        title: '近半年收益率',
        dataIndex: 'sixMonth',
        key: 'sixMonth',
        align: 'right',
      },
      {
        title: '近一年收益率',
        dataIndex: 'oneYear',
        key: 'oneYear',
      },
    ],
  },

  effects: {
    /* 组合概览-  收益率表现子模块 */
    // 收益率风险概览-收益率
    *yieldOrgLevel({ payload }, { call, put }) {
      const res = yield call(api.profitOverview.yieldOrgLevel, payload);
      if (res && res.code === 0) {
        let tempArr = [];
        let obj = {};
        res.data?.map((item) => {
          tempArr.push({ title: item.rangeTypeName, data: parseFloat(item.intervalYield * 100).toFixed(2) + '%' });
          if (item.rangeTypeName == '近一月') {
            obj['oneMonth'] = parseFloat(item.intervalYield * 100).toFixed(2) + '%';
          }
          if (item.rangeTypeName == '近三月') {
            obj['threeMonth'] = parseFloat(item.intervalYield * 100).toFixed(2) + '%';
          }
          if (item.rangeTypeName == '近半年') {
            obj['sixMonth'] = parseFloat(item.intervalYield * 100).toFixed(2) + '%';
          }
          if (item.rangeTypeName == '近一年') {
            obj['oneYear'] = parseFloat(item.intervalYield * 100).toFixed(2) + '%';
          }
        });
        yield put({
          type: 'save',
          payload: {
            yieldOrgLevelData: tempArr,
            yieldOrgLevelData2: [obj],
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            yieldOrgLevelData: [],
            yieldOrgLevelData2: [],
          },
        });
        message.error('收益率数据获取失败，请联系管理员修复。');
        console.error(`收益率数据获取失败，失败原因：${res?.msg}`, '接口名称：该产品的产品代码 接口：portfolio/yieldOrgLevel/');
      }
    },
    // 收益率表现-区间收益率接口
    *getYieldRateData({ payload, callback }, { call, put }) {
      const res = yield call(api.profitOverview.getYieldRateData, payload);
      if (res && res.data && res.code == 0) {
        callback('success', res.data);
      } else {
        message.error('区间收益率数据获取失败，请联系管理员修复。');
        console.error(`区间收益率数据获取失败，失败原因：${res?.msg}`, '接口名称：intervalYieldCmp/ 接口：yieldanalysis/intervalYieldCmp/');
      }
    },
    // 收益率表现-收益率趋势接口
    *getYieldTrend({ payload, callback }, { call, put }) {
      const res = yield call(api.profitOverview.getYieldTrend, payload);
      if (res && res.data && res.code == 0) {
        // 去除超额
        let tempIndex = undefined;
        res?.data?.series &&
          res?.data?.series.forEach((item, index) => {
            if (item.name == '超额') {
              tempIndex = index;
            }
          });
        res?.data?.series.splice(tempIndex, 1);
        callback('success', res.data);
      } else {
        message.error(res.msg || '收益率趋势数据获取失败，请联系管理员修复。');
        console.error(`收益率趋势数据获取失败，失败原因：${res?.msg}`, 'yieldTrendCmp/ 接口：yieldanalysis/yieldTrendCmp/');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
